﻿using UnityEngine;
using System;
using System.Collections.Generic;

namespace Config.Internal
{
    [Serializable]
    public abstract class ConfigBase
    {
        public abstract void DeserializeDataMember();
    }

    [Serializable]
    public abstract class ConfigBase<T> : ConfigBase where T : ConfigDataBase
    {
        protected Dictionary<uint, T> m_Datas = new Dictionary<uint, T>();
        [NonSerialized]
        protected List<T> _list = null;

        public void AddData(T Data)
        {
            if (!(Data is T))
                return;
            if (!m_Datas.ContainsKey(Data.Key()))
                m_Datas.Add(Data.Key(), Data as T);
        }

        public T GetData(uint key)
        {
            if (m_Datas.ContainsKey(key))
                return m_Datas[key];
            //Debug.LogError(string.Format("can't find {0} data by key = {1}", this.ToString(), key));
            return null;
        }
        public List<T> GetDataList()
        {
            if (_list == null)
            {
                _list = new List<T>(m_Datas.Values);
            }
            return _list;
        }

        public override void DeserializeDataMember()
        {
            foreach (var item in m_Datas.Values)
            {
                item.DeserializeMember();
            }
        }
    }

    [Serializable]
    public abstract class ConfigDataBase
    {
        public virtual uint Key()
        {
            return 0;
        }
        public virtual void DeserializeMember()
        {

        }
    }
}
